﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Telerik.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Web.UI.HtmlControls;

/// <summary>
/// List checkbox in radcombo
/// </summary>
namespace Guru.Web.Controls.Telerik
{
    public class GuruComboCheckBox : GuruComboBox
    {
        protected class StringConstants 
        {
            public const string CheckBoxID = "uxItemCheckBox";
        }
        public GuruComboCheckBox()
        {
            this.AllowCustomText = false;
            this.OnClientLoad = "AsCheckBoxCombo_OnClientLoad";            
        }

        #region Properties
        /// <summary>
        /// Get a collection of the currently selected items
        /// </summary>
        public ListItemCollection SelectedItems
        {
            set 
            {
                if (value == null) value = new ListItemCollection();
                foreach (Control ctr in this.Controls)
                {
                    if (ctr is RadComboBoxItem)
                    {
                        RadComboBoxItem cboItem = ctr as RadComboBoxItem;
                        CheckBox chkItem = cboItem.FindControl(StringConstants.CheckBoxID) as CheckBox;
                        if (chkItem != null)
                        {
                            if (value.FindByValue(cboItem.Value) != null)
                            {
                                chkItem.Checked = true;
                            }
                            else 
                            {
                                chkItem.Checked = false;
                            }
                        }
                    }
                }
            }
            get 
            {
                ListItemCollection selectedValues = new ListItemCollection();
                foreach (Control ctr in this.Controls)
                {
                    if (ctr is RadComboBoxItem)
                    {
                        RadComboBoxItem cboItem = ctr as RadComboBoxItem;
                        CheckBox chkItem = cboItem.FindControl(StringConstants.CheckBoxID) as CheckBox;
                        if (chkItem!=null && chkItem.Checked)
                        {
                            ListItem item = new ListItem(cboItem.Text,cboItem.Value);
                            item.Selected = true;
                            item.Enabled = cboItem.Enabled;
                            selectedValues.Add(item);
                        }
                    }
                }
                return selectedValues;
            }
        }   
        /// <summary>
        /// Get or set selected values
        /// </summary>
        public string SelectedValues
        {
            get
            {
                string selectedValues = string.Empty;
               
                ListItemCollection selectedItems = this.SelectedItems;
                foreach (ListItem item in selectedItems)
                {
                    if (string.IsNullOrEmpty(selectedValues) == false)
                    {
                        selectedValues += this.IdSeparator;
                    }
                    selectedValues += item.Value.Trim();
                }
                return selectedValues;
            }
            set
            {
                if (value == null) value = string.Empty;
               
                string[] selectedValues = value.Split(this.IdSeparator);
                foreach (Control ctr in this.Controls)
                {
                    if (ctr is RadComboBoxItem)
                    {
                        RadComboBoxItem cboItem = ctr as RadComboBoxItem;
                        CheckBox chkItem = cboItem.FindControl(StringConstants.CheckBoxID) as CheckBox;
                        if (chkItem != null)
                        {
                            bool selected = false;
                            foreach (string val in selectedValues)
                            {
                                if (val.Trim().Equals(cboItem.Value.Trim(), StringComparison.OrdinalIgnoreCase))
                                {
                                    selected = true;
                                    break;
                                }
                            }
                            chkItem.Checked = selected;
                        }
                    }
                }
            }
        }

        /// <summary>
        /// Show/hide CHECKALL & UNCHECKALL buttons
        /// </summary>
        public bool ShowFooter 
        {
            set 
            {
                ViewState["ShowFooter"] = value;
            }
            get
            {
                if (ViewState["ShowFooter"] == null)
                {
                    return false;
                }
                else 
                {
                    return (bool)ViewState["ShowFooter"];
                }
            }
        }
        /// <summary>
        /// CssClass for CHECKALL & UNCHECKALL buttons
        /// </summary>
        public string FooterButtonCssClass 
        {
            set { ViewState["FooterCssClass"] = value; }
            get 
            {
                if (ViewState["FooterCssClass"] == null)
                {
                    return "rcbFooter_Button";
                }
                else 
                {
                    return ViewState["FooterCssClass"].ToString();
                }
            }
        }
        #endregion

        protected override void InitializeItem(ControlItem item)
        {  
            base.InitializeItem(item);           
            RadComboBoxItem dataItem = item as RadComboBoxItem;
            Panel uxPanelWrapper = new Panel();
            uxPanelWrapper.ID = "uxPanelWrapper";
            
            CheckBox uxItemCheckBox = new CheckBox();
            uxItemCheckBox.ID = StringConstants.CheckBoxID;
            uxItemCheckBox.TextAlign = TextAlign.Right;
            
            uxItemCheckBox.Text = dataItem.Text;

            uxPanelWrapper.Controls.Add(uxItemCheckBox);
            item.Controls.Add(uxPanelWrapper);           
        }       
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            if (this.ShowFooter)
            {
                LinkButton uxFooterSelectAll = new LinkButton();
                uxFooterSelectAll.ID = "uxFooterSelectAll";
                uxFooterSelectAll.Attributes.Add("onclick", string.Format("return AsCheckBoxCombo_FooterCheckBoxClick('{0}',true)", this.ClientID));               
                uxFooterSelectAll.Text = "Check All";
                uxFooterSelectAll.CssClass = this.FooterButtonCssClass;

                LinkButton uxFooterUnselectAll = new LinkButton();
                uxFooterUnselectAll.ID = "uxFooterUnselectAll";
                uxFooterUnselectAll.Attributes.Add("onclick", string.Format("return AsCheckBoxCombo_FooterCheckBoxClick('{0}',false)", this.ClientID));
                uxFooterUnselectAll.Text = "Uncheck All";
                uxFooterUnselectAll.CssClass = this.FooterButtonCssClass;

                this.Footer.Controls.Add(uxFooterSelectAll);
                this.Footer.Controls.Add(uxFooterUnselectAll);                
            }
            // Add client event 
            foreach (Control ctrl in this.Controls)
            {
                if (ctrl is RadComboBoxItem)
                {
                    Panel uxPanelWrapper= ctrl.FindControl("uxPanelWrapper") as Panel;
                    CheckBox uxItemCheckBox = ctrl.FindControl("uxItemCheckBox") as CheckBox;
                    uxPanelWrapper.Attributes.Add("onclick", "AsCheckBoxCombo_StopPropagation(event)");
                    uxItemCheckBox.Attributes.Add("onclick", string.Format("AsCheckBoxCombo_ItemCheckBoxClick(this,'{0}')", this.ClientID));
                }
            }
            GuruComboCheckBox.RegisterResource(this.Page);
        }       
        protected override void OnItemDataBound(RadComboBoxItemEventArgs e)
        {
            base.OnItemDataBound(e);
            CheckBox uxItemCheckBox = e.Item.FindControl(StringConstants.CheckBoxID) as CheckBox; 
            uxItemCheckBox.Text = e.Item.Text;
        }

        public static void RegisterResource(Page page) 
        {
            // Regist javascript
            string clientScript = page.ClientScript.GetWebResourceUrl(typeof(GuruComboCheckBox), "Guru.Web.Controls.EmbeddedResource.GuruComboCheckBox.js");
            page.ClientScript.RegisterClientScriptInclude("Guru.Web.Controls.EmbeddedResource.GuruComboCheckBox.js", clientScript);

            // Regist CSS
            HtmlLink cssLink = new HtmlLink();
            cssLink.Href = cssLink.Href = page.ClientScript.GetWebResourceUrl(typeof(GuruComboCheckBox), "Guru.Web.Controls.EmbeddedResource.GuruComboCheckBox.css");
            cssLink.Attributes.Add("rel", "stylesheet");
            cssLink.Attributes.Add("type", "text/css");
            page.Header.Controls.Add(cssLink);
        }
    }
}
